package com.example.springboot.controller;

import com.auth0.jwt.JWT;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseController {
    @Resource
    private UserMapper userMapper;

    @Resource
    private HttpServletRequest request;

    /**
     * 根据token获取用户信息
     * @return
     */
    public User getUser(){
        String token = request.getHeader("token");
        String aud = JWT.decode(token).getAudience().get(0);
        Integer userId = Integer.valueOf(aud);
        return userMapper.selectById(userId);
    }
}
